/**
 * @PROJECT 
 * @DATE 2018年2月18日 下午10:22:43
 * @AUTHOR LUYU
 */
package com.kool.evowkf.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kool.core.CoreConstants;
import com.kool.core.cache.KCache;
import com.kool.core.context.ContextHolder;
import com.kool.core.exception.AppException;
import com.kool.evowkf.api.IEvoWkfApi;
import com.kool.evowkf.api.SimpleApiImp;
import com.kool.evowkf.bean.SyWkfRecordBean;
import com.kool.evowkf.bean.io.CMCHECKXBean;
import com.kool.evowkf.bean.io.approve.CMAPPROVEXBean;
import com.kool.evowkf.bean.io.approve.CMQRYSCHEDULEXBean;
import com.kool.evowkf.dao.SyWkfRecordDao;
import com.kool.evowkf.engine.WKFContext;
import com.kool.system.bean.SyUserInfoBean;
import com.kool.system.bean.io.SYPAGEINFOYBean;
import com.kool.system.bean.vo.PageResult;

/**
 * @DESCRIBE 工作流业务处理类
 * @AUTHOR LUYU
 * @DATE 2018年2月18日 下午10:22:43
 *
 */
@Service
public class WkfService {
	private static Logger logger = Logger.getLogger(WkfService.class);
	
	@Autowired
	private SyWkfRecordDao recordDao;
	
	/**
	 * @DESCRIBE
	 * @DATE 2018年2月19日 下午12:02:13
	 *
	 * @param scheduleInfo
	 * @param pageInfo
	 * @return
	 */
	public PageResult querySchedule(CMQRYSCHEDULEXBean scheduleInfo, SYPAGEINFOYBean pageInfo) {
		String wkfNbr = scheduleInfo.getWkfNbr();
		String businessType = scheduleInfo.getBusinessType();

		String userCid = ContextHolder.getReqContext().getUserCid();
		SyUserInfoBean user = (SyUserInfoBean) KCache.get(CoreConstants.KCACHE_USER, userCid);
		String loginName = user != null ? user.getSuiLoginName() : null;

		IEvoWkfApi api = new SimpleApiImp();
		PageResult pageResult = api.querySchedule(wkfNbr, businessType, userCid, loginName, pageInfo);

		return pageResult;
	}

	/**
	 * 
	 * @DESCRIBE 审批
	 * @DATE 2018年2月19日 下午3:01:43
	 *
	 * @param approveInfo
	 * @throws AppException
	 * @throws BusException
	 */
	public void approve(CMAPPROVEXBean approveInfo) throws AppException {
		String scheduleId = approveInfo.getScheduleCid();
		String wkfNbr = approveInfo.getWkfNbr();
		String approveFlag = approveInfo.getEventFlag();
		String dsp = approveInfo.getEventDsp();

		String userCid = ContextHolder.getReqContext().getUserCid();
		SyUserInfoBean user = (SyUserInfoBean) KCache.get(CoreConstants.KCACHE_USER, userCid);
		String loginName = user != null ? user.getSuiLoginName() : null;
		WKFContext wkfInfo = new WKFContext();
		wkfInfo.setWkfNbr(wkfNbr);
		wkfInfo.setScheduleId(scheduleId);
		wkfInfo.setApproveFlag(approveFlag);
		wkfInfo.setDescription(dsp);
		wkfInfo.setUserId(userCid);
		wkfInfo.setLoginName(loginName);
		IEvoWkfApi api = new SimpleApiImp();
		api.nextStep(wkfInfo);
	}

	/**
	 * @DESCRIBE 签收
	 * @DATE 2018年2月20日 上午10:03:20
	 *
	 * @param checkInfo
	 * @throws BusException
	 */
	public void check(CMCHECKXBean checkInfo) throws AppException {
		String recordId = checkInfo.getWkfNbr();
		String scheduleId = checkInfo.getScheduleCid();
		String userCid = ContextHolder.getReqContext().getUserCid();
		SyUserInfoBean user = (SyUserInfoBean) KCache.get(CoreConstants.KCACHE_USER, userCid);
		String loginName = user != null ? user.getSuiLoginName() : null;
		IEvoWkfApi api = new SimpleApiImp();
		api.check(recordId, scheduleId, userCid, loginName);
	}

	/**
	 * @DESCRIBE 分页查询流程执行记录
	 * @DATE 2018年7月22日 下午7:48:38
	 *
	 * @param recInfo
	 * @param pageInfo
	 * @return
	 */
	public PageResult queryRecordListPage(SyWkfRecordBean recInfo, SYPAGEINFOYBean pageInfo) {
		logger.debug(recInfo.toString());
		int total = recordDao.selectTotal(recInfo);

		int currentPage = pageInfo.getCurrentPage();
		int pageSize = pageInfo.getPageSize();
		int offset = (currentPage - 1) * pageSize;
		int pageCount = (total + pageSize - 1) / pageSize;
		List<SyWkfRecordBean> list = recordDao.selectPage(recInfo, offset, pageInfo.getPageSize());

		pageInfo.setTotal(total);
		pageInfo.setPageCount(pageCount);

		PageResult pageResult = new PageResult();
		pageResult.setListBusinessValue(new ArrayList<>(list));
		pageResult.setPageInfo(pageInfo);
		return pageResult;
	}
}
